/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.dao.services.AccountDAOServices;
import model.enetities.Account;
import model.enetities.Role;
import model.enetities.UserProfile;

/**
 *
 * @author Ku81
 */
public class AccountDAO implements AccountDAOServices {

    private static AccountDAO accountDAO;

    Connection conn;
    public static AccountDAO getInstance() {
        if (accountDAO == null) {
            accountDAO = new AccountDAO();

        }
        return accountDAO;
    }

    @Override
    public List<Account> getAccounts() {
        List<Account> accountList = new ArrayList<Account>();
        String sql = "SELECT * FROM tblAccount WHERE isActived='true'";
        PreparedStatement pstmt;
        try {
            conn = ConnectionFactory.getConnection();
            pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Account acc = new Account();
                acc.setActived(rs.getBoolean("isActived"));
                acc.setIdAccount(rs.getInt("idAccount"));
                Role role = RoleDAO.getInstance().getRoleByID(rs.getInt("idRole"));
                acc.setIdRole(role);
                acc.setPassword(rs.getString("password"));
                acc.setUserName(rs.getString("userName"));
                accountList.add(acc);
            }
        } catch (SQLException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return accountList;
    }

    @Override
    public Account getAccountByID(int idAccount) {
        String sql = "SELECT * FROM tblAccount WHERE idAccount =? AND isActived='true'";
        Account acc = new Account();
        try {
            conn = ConnectionFactory.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, idAccount);
            ResultSet rs = ps.executeQuery();
            acc.setActived(true);
            acc.setIdAccount(rs.getInt("idAccount"));
            Role role = RoleDAO.getInstance().getRoleByID(rs.getInt("idRole"));
            acc.setIdRole(role);
            acc.setPassword(rs.getString("password"));
            acc.setUserName(rs.getString("userName"));
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return acc;
    }

    @Override
    public Account getAccountByName(String userName) {
        String sql = "SELECT * FROM tblAccount WHERE userName =? AND isActived='true'";
        Account acc = new Account();
        try {
            conn = ConnectionFactory.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, userName);
            System.out.println("------------------- "+userName);
            ResultSet rs = ps.executeQuery();
            
            acc.setActived(rs.getBoolean("isActived"));
            acc.setIdAccount(rs.getInt("idAccount"));
            Role role = RoleDAO.getInstance().getRoleByID(rs.getInt("idRole"));
            acc.setIdRole(role);
            acc.setPassword(rs.getString("password"));
            acc.setUserName(rs.getString("userName"));
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return acc;
    }

    @Override
    public Role getRoleByID(int idRole) {
        Role role = new Role();
        String sql = "SELECT * FROM tblRole WHERE idRole=? AND isActived='true'";
        try {
            conn = ConnectionFactory.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            role.setIdRole(rs.getInt("idRole"));
            role.setName(rs.getString("name"));
            role.setDescriptions(rs.getString("descriptions"));
            role.setActived(rs.getBoolean("isActived"));
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return role;
    }

    @Override
    public Role getRoleByName(String nameRole) {
        Role role = new Role();
        String sql = "SELECT * FROM tblRole WHERE name=? AND isActived='true'";
        try {
            conn = ConnectionFactory.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            role.setIdRole(rs.getInt("idRole"));
            role.setName(rs.getString("name"));
            role.setDescriptions(rs.getString("descriptions"));
            role.setActived(rs.getBoolean("isActived"));
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return role;
    }

    @Override
    public boolean createAccount(Account account) {

        boolean isOk = false;
        try {
            conn = ConnectionFactory.getConnection();
            String sql = "INSERT INTO tblAccount VALUES(?,?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);

            pstmt.setString(1, account.getUserName());
            pstmt.setString(2, account.getPassword());
            Role r = RoleDAO.getInstance().getRoleByID(2);
            pstmt.setInt(3, r.getIdRole());
            pstmt.setBoolean(4, account.isActived());
            pstmt.executeUpdate();
            isOk = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isOk;
    }

    @Override
    public boolean updateAccount(Account account) {
        try {
            String sql = "UPDATE tblAccount SET password =? , idRole =?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, account.getPassword());
            ps.setInt(2, account.getIdRole().getIdRole());
            ps.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public boolean checkLogin(Account account) {
        String sql = "SELECT * FROM tblAccount WHERE userName=? AND password=?";

        try {
            conn = ConnectionFactory.getConnection();
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, account.getUserName());
            pstmt.setString(2, account.getPassword());
            ResultSet rs = pstmt.executeQuery();
            if(rs.next() == true){
                return true;
            }
                
        } catch (Exception e) {
            e.toString();
        }
        return false;
        
        
    }

    @Override
    public boolean deleteAccount(int idAccount) {
        try {
            String sql = "UPDATE tblAccount SET isActived = 'false' WHERE idAccount=?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, idAccount);
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;

    }

    // Kiem tra tinh duy nhat cua Account
    @Override
    public boolean onlyAccount(String account) {
        String sql = "SELECT * FROM tblAccount WHERE userName=?";

        try {
            conn = ConnectionFactory.getConnection();
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, account);
            ResultSet rs = pstmt.executeQuery();
            return rs.next();
        } catch (Exception e) {
        }
        return true;
    }

}
